import Vue from 'vue'
import Router from 'vue-router'
import { getCookie } from '../util/index'
import login from '@/views/login/login'
// pages
const tAdd1 = resolve => require(['@/views/deal/tAdd1'], resolve)
const tAddD = resolve => require(['@/views/deal/tAddD'], resolve)
const find = resolve => require(['@/views/find/find'], resolve)
const home = resolve => require(['@/views/home/home'], resolve)
const register1 = resolve => require(['@/views/login/register1'], resolve)
const register2 = resolve => require(['@/views/login/register2'], resolve)
const forget = resolve => require(['@/views/login/forget'], resolve)
const my = resolve => require(['@/views/my/my'], resolve)
const toGuide = resolve => require(['@/views/my/toGuide'], resolve)
const understand = resolve => require(['@/views/my/understand'], resolve)
const toAccountinformation = resolve => require(['@/views/my/toAccountinformation'], resolve)
const toNotices = resolve => require(['@/views/my/toNotices'], resolve)
const ruleA = resolve => require(['@/views/my/ruleA'], resolve)
const Search = resolve => require(['@/views/find/Search'], resolve)
const ruleTD = resolve => require(['@/views/my/ruleTD'], resolve)
const safe = resolve => require(['@/views/my/safe'], resolve)
const about = resolve => require(['@/views/my/about'], resolve)
const noticesDetail = resolve => require(['@/views/my/noticesDetail'], resolve)
const toServiceCenter = resolve => require(['@/views/my/toServiceCenter'], resolve)                
const toRecharge = resolve => require(['@/views/my/toRecharge'], resolve)
const toWithdrawals = resolve => require(['@/views/my/toWithdrawals'], resolve)     
const quickpay = resolve => require(['@/views/my/quickpay'], resolve)
const alipayDeatil = resolve => require(['@/views/my/alipayDeatil'], resolve) 
const problem = resolve => require(['@/views/my/problem'], resolve)
const alipay = resolve => require(['@/views/my/alipay'], resolve)
const addCard = resolve => require(['@/views/my/addCard'], resolve)
const changePassword = resolve => require(['@/views/my/changePassword'], resolve)         
const toFunddetails = resolve => require(['@/views/my/toFunddetails'], resolve)
const peopleNum = resolve => require(['@/views/find/peopleNum'], resolve)
const findWithdrawals = resolve => require(['@/views/my/findWithdrawals'], resolve)  
const bankList = resolve => require(['@/views/my/bankList'], resolve) 
const toBuy = resolve => require(['@/views/deal/toBuy'], resolve)  
const realnameInfo = resolve => require(['@/views/my/realnameInfo'], resolve) 
const myInviteCode = resolve => require(['@/views/my/myInviteCode'], resolve)
const optional = resolve => require(['@/views/optional/optional'], resolve)   
const dealDetail = resolve => require(['@/views/deal/dealDetail'], resolve)  
const sellDetail = resolve => require(['@/views/deal/sellDetail'], resolve)
const error = resolve => require(['@/views/error/error'], resolve)

Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/login',
      name: 'login',
      component: login,
    },
    {
      path: '/',
      name: 'home',
      component: home,
    },
    {
      path: '/forget',
      name: 'forget',
      component: forget
    },
    {
      path: '/register1',
      name: 'register1',                  
      component: register1,
      
    },
    {
      path: '/register2',
      name: 'register2',
      component: register2,
     
    },
    {
      path: '/tAdd1',
      name: 'tAdd1',
      component: tAdd1,
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/tAddD',
      name: 'tAddD',
      component: tAddD,
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/find',
      name: 'find',
      component: find,
      meta: {
        requireAuth: true
      }
    },
    
    {
      path: '/my',
      name: 'my',
      component: my,
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/toGuide',
      name: 'toGuide',
      component: toGuide,
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/toWithdrawals',
      name: 'toWithdrawals',
      component: toWithdrawals,
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/toRecharge',
      name: 'toRecharge',
      component: toRecharge,
      meta: {
        requireAuth: true  
      }
    },
    {
      path: '/toAccountinformation',
      name: 'toAccountinformation',
      component: toAccountinformation,                
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/findWithdrawals',
      name: 'findWithdrawals',
      component: findWithdrawals,
      meta: {
        requireAuth: true                                          
      }
    },
    {
      path: '/peopleNum',
      name: 'peopleNum',
      component: peopleNum,
      meta: {
        requireAuth: true                         
      }
    },
    {
      path: '/dealDetail',
      name: 'dealDetail',
      component: dealDetail,
      meta: {
        requireAuth: true                         
      }
    },
    {
      path: '/toServiceCenter',
      name: 'toServiceCenter',
      component: toServiceCenter,
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/toNotices',
      name: 'toNotices',
      component: toNotices,   
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/toBuy',
      name: 'toBuy',
      component: toBuy,
      meta: {
        requireAuth: true                     
      }
    },
    {
      path: '/noticesDetail',
      name: 'noticesDetail',
      component: noticesDetail,
      meta: {
        requireAuth: true                     
      }
    },
    {
      path: '/Search',
      name: 'Search',
      component: Search,
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/understand',
      name: 'understand',
      component: understand,
      meta: {
        requireAuth: true               
      }
    },
    {
      path: '/ruleA',
      name: 'ruleA',
      component: ruleA,
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/problem',
      name: 'problem',
      component: problem,
      meta: {
        requireAuth: true
      }
    },
     {
      path: '/ruleTD',
      name: 'ruleTD',
      component: ruleA,
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/safe',
      name: 'safe',
      component: safe,
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/about',
      name: 'about',
      component: about,
      meta: {
        requireAuth: true
      }
    },                                              
    {
      path: '/toFunddetails',
      name: 'toFunddetails',
      component: toFunddetails,                           
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/bankList',
      name: 'bankList',
      component: bankList,                           
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/myInviteCode',
      name: 'myInviteCode',
      component: myInviteCode,
      meta: {
        requireAuth: true
      }
    },
    
    {
      path: '/quickpay',
      name: 'quickpay',
      component: quickpay,
      meta: {
        requireAuth: true               
      }
    },
    {
      path: '/changePassword',
      name: 'changePassword',
      component: changePassword,
      meta: {
        requireAuth: true
      }
    },
     {
      path: '/realnameInfo',
      name: 'realnameInfo',
      component: realnameInfo,
      meta: {
        requireAuth: true
      }
    },
    {                                                                
      path: '/alipay',
      name: 'alipay',
      component: alipay,
      meta: {
        requireAuth: true
      }
    },
    {                                                                 
      path: '/sellDetail',
      name: 'sellDetail',
      component: sellDetail,
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/addCard',
      name: 'addCard',
      component: addCard,
      meta: {
        requireAuth: true
      }
    },
    {                                                   
      path: '/optional',
      name: 'optional',
      component: optional,
      meta: {
        requireAuth: true
      }
    },
    {
      path: '/alipayDeatil',
      name: 'alipayDeatil',
      component: alipayDeatil,
      meta: {
        requireAuth: true
      }
    },
    {
      path: '*',
      name: 'error',
      component: error          
    }
  ]
})

router.beforeEach((to, from, next) => {
  let token = localStorage.getItem("token");
  if (to.meta.requireAuth) {
    if (token) {
      next()
    } else {
      next({
        path: '/login'
      })
    }
  } else {
    next()
  }
})

export default router